package com.users.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.enums.ResultCode;
import com.users.entity.User;
import com.users.service.UserService;
import com.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hy
 * @since 2021-05-10
 */
@RestController
@RequestMapping("/users")
public class UserLoginController {
    @Autowired
    private UserService userService;
    /**
     * 用户注册
     */
    @PostMapping("register")
    public R userRegister(@RequestBody User user){
        //先检查是不是我们学校的
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",user.getName());
        queryWrapper.eq("six",user.getSix());
        User queryUser = userService.getOne(queryWrapper);
        if (!ObjectUtils.isEmpty(queryUser)) {//如果是我们学校的，则允许注册
            queryUser.setStuno(user.getStuno());
            queryUser.setIsActive(true);
            userService.update(queryUser,null);
            return R.ok("恭喜你，注册成功！！！");
        }else {
            return R.error("系统检测到您不是南昌工程学院2018级或2019级学生，注册失败！！！");
        }
    }

    /**
     * 用户登录
     */
    @PostMapping("login")
    public R userLogin(@RequestBody User user){
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("stuno",user.getStuno());
        User queryUser = userService.getOne(queryWrapper);
        if (!ObjectUtils.isEmpty(queryUser)) { //查到了
            if (queryUser.getSix().equals(user.getSix())) {
                return R.ok("登录成功");
            }else {
                return R.error("用户名或密码错误");
            }
        }else {
            return R.error(ResultCode.UNREGISTERED,"您还未注册");
        }
    }

    /**
     *
     */
}

